<template>
  <div>
    <div class="page-form">
      <el-select
        v-hasRole="['admin']"
        v-model="queryParams.examSchoolId"
        placeholder="选择学校"
        clearable
        @change="handleSearch"
      >
        <el-option
          v-for="item in xxlb"
          :key="item.id"
          :label="item.schoolName"
          :value="item.id"
        >
        </el-option>
      </el-select>
      <!-- <el-select
        v-model="queryParams.classId"
        placeholder="选择状态"
        clearable
        @change="handleSearch"
      >
      </el-select> -->
      <el-select
        v-model="queryParams.examSubjectId"
        placeholder="选择科目"
        clearable
        @change="handleSearch"
      >
        <el-option
          v-for="item in kmlb"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        ></el-option>
      </el-select>
    </div>
    <el-table
      stripe
      border
      ref="tableList"
      v-loading="loading"
      :data="tableData"
      style="width: 100%"
      row-key="id"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column
        type="selection"
        width="55"
        align="center"
        :reserve-selection="true"
      /> -->
      <el-table-column
        label="学校名称"
        align="center"
        prop="examSchoolName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="考试"
        align="center"
        prop="subjectName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="上报人数"
        align="center"
        prop="reportedNumber"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="已上传"
        align="center"
        prop="uploadedNumber"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="未上传"
        align="center"
        prop="notUploadedNumber"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="考号异常"
        align="center"
        prop="examNumberAbnormal"
        :show-overflow-tooltip="true"
        class-name="color-danger"
      />
      <el-table-column
        label="上传比例"
        align="center"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          {{ scope.row.uploadProgress }}%
        </template>
      </el-table-column>
      <el-table-column
        label="缺考人数"
        align="center"
        prop="missingExamNumber"
        :show-overflow-tooltip="true"
        class-name="color-danger"
      />
      <el-table-column
        label="选项异常"
        align="center"
        prop="optionAbnormalNumber"
        :show-overflow-tooltip="true"
        class-name="color-danger"
      />
      <el-table-column
        label="操作"
        align="center"
        width="220"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-link
            type="success"
            @click="sctk(scope.row, scope.$index)"
            v-if="scope.row.uploadedNumber > 0 && scope.row.downStatus == 1"
            >重新生成</el-link
          >
          <el-link
            type="success"
            @click="sctk(scope.row, scope.$index)"
            v-if="scope.row.uploadedNumber > 0 && scope.row.downStatus == 0"
            >生成题卡</el-link
          >
          <el-link
            type="primary"
            @click="xztk(scope.row)"
            v-if="scope.row.uploadedNumber > 0 && scope.row.downStatus == 1"
            >下载题卡</el-link
          >
          <el-link type="success" v-if="scope.row.downStatus == 2"
            >生成中 {{ downProgress }}%</el-link
          >
          <el-link type="primary" @click="xq(scope.row)">查看详情</el-link>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-bottom">
      <div class="sel-box">
        <p>共 {{ total }} 项</p>
        <el-button type="primary" @click="getList()" :loading="loading"
          >刷新</el-button
        >
      </div>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import {
  scanQuestionCardSchoolList,
  scanQuestionCardSchoolListSchool,
  downScanZipQuery,
  downScanZipQueryStatus,
  downScanZip,
} from "@/api/xmgl/tkcx";
import { debounce } from "lodash";
export default {
  props: {
    xmxx: {},
    kmlb: null,
  },
  data() {
    return {
      total: 0,
      ids: [],
      tableData: [],
      multiple: true,
      loading: false,
      queryParams: {
        pageNum: 1,
        pageSize: 15,
      },
      xxlb: [],

      imageLoading: false,

      downProgress: 0,
      downIndex: null,

      timer: null,
    };
  },
  created() {
    scanQuestionCardSchoolListSchool({
      examId: this.xmxx.id,
    }).then((res) => {
      this.xxlb = res.rows;

      if (!this.$auth.hasRole("admin")) {
        this.queryParams.examSchoolId = this.xxlb.filter((item) => {
          return this.$store.state.user.deptId == item.schoolId;
        })[0].id;
      }

      if (
        !this.$auth.hasRole("admin") &&
        !this.$auth.hasExamPermi("reviewing:teachers:tkcx") &&
        this.$auth.hasExamPermi("reviewing:teachers:tkcxkm")
      ) {
        this.queryParams.examSubjectIds =
          this.$store.state.xmgl.examSubjects.map((item) => {
            return item.examSubjectId;
          });
      }

      this.getList();
    });
  },
  methods: {
    debouncedSearch: debounce(function () {
      this.queryParams.pageNum = 1;
      this.getList();
    }, 500),
    handleSearch() {
      this.debouncedSearch();
    },
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.multiple = !selection.length;
    },
    getList() {
      if (this.timer) {
        this.endTime();
      }
      this.loading = true;
      this.tableData = [];
      scanQuestionCardSchoolList({
        ...this.queryParams,
        examId: this.xmxx.id,
      })
        .then((response) => {
          this.total = response.data.total;
          this.tableData = response.data.records;

          let downIndex = this.tableData.findIndex((item) => {
            return item.downStatus == 2;
          });

          if (downIndex != -1) {
            this.downIndex = downIndex;
            this.downScanZipQuery();
            this.startTime();
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    xq(row) {
      this.$store.dispatch("app/setIsRefresh", true).then((res) => {
        this.$router.push({
          path: "/kwgl/tkxq/index",
          query: {
            subjectId: row.subjectId,
            examSchoolId: row.examSchoolId,
          },
        });
      });
    },
    sctk(row, index) {
      this.loading = true;
      downScanZipQueryStatus()
        .then((res) => {
          if (!res.data) {
            downScanZip({
              examId: this.xmxx.id,
              examSubjectId: row.subjectId,
              examSchoolId: row.examSchoolId,
            })
              .then((res) => {
                this.downProgress = 0;
                this.downIndex = index;
                this.tableData[this.downIndex].downStatus = 2;
                this.startTime();
              })
              .finally(() => {
                this.loading = false;
              });
          } else {
            this.loading = false;
            this.$modal.alert("目前有正在生成题卡中的数据，请您稍后再试");
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },
    xztk(row) {
      window.open(row.zipUrl);
    },
    startTime() {
      this.timer = setInterval(() => {
        this.downScanZipQuery();
      }, 2000);
    },
    downScanZipQuery() {
      downScanZipQuery({
        examSchoolId: this.tableData[this.downIndex].examSchoolId,
        examSubjectId: this.tableData[this.downIndex].subjectId,
        examId: this.xmxx.id,
      }).then((res) => {
        if (res.data.nowProcess != 100) {
          this.downProgress = res.data.nowProcess;
        } else {
          this.tableData[this.downIndex].downStatus = 1;
          this.tableData[this.downIndex].zipUrl =
            res.data.examScanSchoolCardZip.zipUrl;
          this.endTime();
        }
      });
    },
    endTime() {
      this.downProgress = 0;
      this.downIndex = null;
      clearInterval(this.timer);
      this.timer = null;
    },
  },
  beforeDestroy() {
    if (this.timer) {
      this.endTime();
    }
  },
};
</script>

<style>
</style>